import type { App } from "vue"
import {auth as useAuth} from "@/store"
import router from "@/router"
import {authRouteMode,authBtnMode} from "@/common/config"

/**
 * @param authKeys 按钮权限值
 * @returns true or false是否具备某个按钮的权限
 */
export const hasAuth = (authKeys: string[]) => {
  if(authRouteMode=='local') return true
  const auth=useAuth()
  if(authBtnMode=='request')
  return authKeys.some(v=>auth.accessRequestNameList.includes(v))
  else 
  { 
    const route = router.currentRoute.value
    const actionList:any = route.meta.actionList
    if(!actionList) return true 
    return authKeys.some(v=>actionList.includes(v))
  }
}

/**
 * 
 * @param hasFooter 对话框是否包含footer
 * @returns 对话框body可以占据的最大高度
 */
export const getDialogBodyMaxHeight=(hasFooter=true)=>{
  //54是modal的header占据的高度，62是modal的footer占据的高度,100是给屏幕留白的高度。如果el-scroll存在兼容性问题，请用 modalBodyMaxHeigth的overflow方案
  if(hasFooter)
  return document.documentElement.clientHeight-(54+62+100) 
  else
  return document.documentElement.clientHeight-(54+100) 
}

export default (app: App) => {
  app.config.globalProperties.$window = window
  app.config.globalProperties.$hasAuth = hasAuth
  app.config.globalProperties.$getDialogBodyMaxHeight = getDialogBodyMaxHeight
}